package com.t174.car.shiro;

import com.t174.car.dao.RoleDao;
import com.t174.car.dao.UserInfoDao;
import com.t174.car.entity.Role;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class MyRealm extends AuthorizingRealm {
    @Autowired
    private UserInfoDao userInfoDao ;
    @Autowired
    private RoleDao roleDao ;
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了授权方法...");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo() ;
        String username = (String) principalCollection.getPrimaryPrincipal() ;
        Role role = roleDao.getRoleByUname(username) ;
        return null;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken ;
        String username = token.getUsername() ;
        //1.根据用户名从数据库表中获取用户的密码
        String pwd = userInfoDao.getPwdByUserName(username) ;
        System.out.println(pwd);
        if(pwd==null|| "".equals(pwd)){
            return  null ;
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username,pwd,"myrealm") ;
        return info;
    }

}
